package com.javaee.controller.doctor;

import com.javaee.BeforeBaseController;
import com.javaee.entity.Doctor;

import com.javaee.service.doctor.DRecordService;
import com.javaee.service.doctor.SelectMzghService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/doctor")
public class SelectMzghController extends BeforeBaseController {
    @Autowired
    private SelectMzghService selectMzghService;
    @Autowired
    private DRecordService dRecordService;

    @RequestMapping("/selectMzgh")
    public String listDMzghs(Model model,Integer currentPage, HttpSession session) {
        if (currentPage == null) {
            currentPage = 1; // 默认为第一页
        }
        Doctor doctor = (Doctor) session.getAttribute("Doctor");
        int id = doctor.getId();
        int totalCount = selectMzghService.selectCount();
        int pageSize = 5;
        int totalPage = (int)Math.ceil(totalCount*1.0/pageSize);
        List<Map<String,Object>> mzghs = selectMzghService.findAll((currentPage-1)*pageSize, pageSize,id);
        model.addAttribute("mzghs", mzghs);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currentPage", currentPage);
        List<Integer> existingRecordMzghIds = dRecordService.findAllMIds();
        model.addAttribute("existingRecordMzghIds", existingRecordMzghIds);
        return "doctor/call";
    }

    @PostMapping("/call")
    public String callPatient(@RequestParam int id, Model model, HttpSession session) {
        // 假设叫号状态为'1'
        selectMzghService.updateStatus(id, "1");
        // 可能需要重定向或刷新页面
        return "redirect:/doctor/selectMzgh"; // 重定向到挂号列表页面
    }

    @PostMapping("/cancelCall")
    public String cancelCall(@RequestParam int id, Model model, HttpSession session) {
        // 假设取消叫号状态为'0'
        selectMzghService.updateStatus(id, "0");
        // 可能需要重定向或刷新页面
        return "redirect:/doctor/selectMzgh"; // 重定向到挂号列表页面
    }

    @GetMapping("/searchByDetailAndYyrq")
    public String searchByDetailAndYyrq(@RequestParam String detail, @RequestParam String yyrq, Model model,Integer currentPage, HttpSession session) {
        if (currentPage == null) {
            currentPage = 1; // 默认为第一页
        }
        Doctor doctor = (Doctor) session.getAttribute("Doctor");
        int id=doctor.getId();
//        int doctorId = doctor != null ? doctor.getId() : -1; // 检查 doctor 对象是否为 null
        int totalCount = selectMzghService.selectCount();
        int pageSize = 5;
        int totalPage = (int)Math.ceil(totalCount*1.0/pageSize);
        List<Map<String, Object>> mzghs = selectMzghService.findByDetailAndYyrq(detail, yyrq,id, (currentPage - 1) * pageSize, pageSize);
        model.addAttribute("mzghs", mzghs);
        model.addAttribute("searchDetail", detail);
        model.addAttribute("searchYyrq", yyrq);
        model.addAttribute("totalPage", totalPage);
        model.addAttribute("currentPage", currentPage);
        List<Integer> existingRecordMzghIds = dRecordService.findAllMIds();
        model.addAttribute("existingRecordMzghIds", existingRecordMzghIds);
        return "doctor/call";
    }
}